package helpdesk;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/*
 * This class manages our forum posts
 * @author MattLampe
 */
public class ForumGUI extends javax.swing.JFrame
{
    Post[] data;
    User user = null;
    
    /**
     * Creates new form ForumGUI
     */
    public ForumGUI()
    {
        initComponents();
    }
    
    public ForumGUI(ArrayList<Post> post, User Jim)
    {
        // mainly fills out the JTable
        data = new Post[post.size()];
        initComponents();
        user = Jim;
        
        forumTable.getColumnModel().getColumn(0).setHeaderValue("Author");
        forumTable.getColumnModel().getColumn(1).setHeaderValue("Title");
        forumTable.getColumnModel().getColumn(2).setHeaderValue("Body");
        
        // set each value in the table
        for(int i = 0; i < data.length; i++)
        {
            data[i] = post.get(i);
            
            forumTable.setValueAt(data[i].author, i, 0);
            forumTable.setValueAt(data[i].title, i, 1);
            forumTable.setValueAt(data[i].body, i, 2);
        }
        
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        forumTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        CancelButton = new javax.swing.JButton();
        replyButton = new javax.swing.JButton();
        ViewThreadButton = new javax.swing.JButton();
        deletePostButton = new javax.swing.JButton();
        editPostButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        forumTable.setModel(new AbstractTableModel()
            {
                @Override
                public int getRowCount()
                {
                    return data.length;
                }

                @Override
                public int getColumnCount()
                {
                    return 3;
                }

                @Override
                public String getValueAt(int i, int j)
                {
                    String temp = null;
                    switch (j)
                    {
                        case 0:
                        temp = data[i].author;
                        break;
                        case 1:
                        temp = data[i].title;
                        break;
                        case 2:
                        temp = data[i].body;
                        break;
                    }
                    return temp;
                }

                public void setValueAt(String val, int i, int j)
                {
                    switch (j)
                    {
                        case 1:
                        data[i].author = val;
                        break;
                        case 2:
                        data[i].title = val;
                        break;
                        case 3:
                        data[i].body = val;
                        break;
                    }
                }
            });
            jScrollPane1.setViewportView(forumTable);

            jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
            jLabel1.setText("Forum");

            CancelButton.setText("Cancel");
            CancelButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    CancelButtonActionPerformed(evt);
                }
            });

            replyButton.setText("Reply");
            replyButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    replyButtonActionPerformed(evt);
                }
            });

            ViewThreadButton.setText("View");
            ViewThreadButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ViewThreadButtonActionPerformed(evt);
                }
            });

            deletePostButton.setText("Delete");
            deletePostButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    deletePostButtonActionPerformed(evt);
                }
            });

            editPostButton.setText("Edit");
            editPostButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    editPostButtonActionPerformed(evt);
                }
            });

            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(394, 394, 394)
                            .add(jLabel1)
                            .add(0, 419, Short.MAX_VALUE))
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(layout.createSequentialGroup()
                                    .add(0, 0, Short.MAX_VALUE)
                                    .add(ViewThreadButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(editPostButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(deletePostButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(replyButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(CancelButton))
                                .add(jScrollPane1))))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(68, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(18, 18, 18)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 368, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(CancelButton)
                        .add(replyButton)
                        .add(ViewThreadButton)
                        .add(deletePostButton)
                        .add(editPostButton))
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        
        this.dispose();
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void replyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replyButtonActionPerformed
        // if something was selected
        if (forumTable.getSelectedRow() >= 0)
        {
            // bring up a reply to that window
            Post info = data[forumTable.getSelectedRow()];
            AddPost replyGUI = new AddPost(info, user);
            replyGUI.setVisible(true);
        }
    }//GEN-LAST:event_replyButtonActionPerformed

    // refreshes forum post info
    private void updatePostData()
    {
        MongoDB.getRecentPosts(user).toArray(data);
    }
    
    private void ViewThreadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewThreadButtonActionPerformed
        // if a selection was made, bring up it's thread
        if (forumTable.getSelectedRow() >= 0)
        {
            Post temp = data[forumTable.getSelectedRow()];
            new ViewThreadGUI(temp, user);
        }
    }//GEN-LAST:event_ViewThreadButtonActionPerformed

    // deletes a selected post
    private void deletePostButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletePostButtonActionPerformed
        if (forumTable.getSelectedRow() >= 0)
        {
            Post temp = data[forumTable.getSelectedRow()];
            if (user.username.equals(temp.author))
            {
                MongoDB.deletePost(temp.postId);
            }
            else
            {
                System.out.println("This was not your post");
            }
        }
    }//GEN-LAST:event_deletePostButtonActionPerformed

    // edits selected post
    private void editPostButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editPostButtonActionPerformed
        if (forumTable.getSelectedRow() >= 0)
        {
            Post temp = data[forumTable.getSelectedRow()];
            if (user.username.equals(temp.author))
            {
                EditPost eP = new EditPost(temp, user);
                eP.setVisible(true);
            }
            else
            {
                System.out.println("This was not your post");
            }
        }
    }//GEN-LAST:event_editPostButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ForumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ForumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ForumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ForumGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new ForumGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton ViewThreadButton;
    private javax.swing.JButton deletePostButton;
    private javax.swing.JButton editPostButton;
    private javax.swing.JTable forumTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton replyButton;
    // End of variables declaration//GEN-END:variables
}
